<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission"/>
      <!--表单组件-->
      <el-dialog
        :before-close="crud.cancelCU"
        :close-on-click-modal="false"
        :title="crud.status.title"
        :visible.sync="crud.status.cu > 0"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
          <el-form-item label="部署类型" prop="deployType">
            <el-radio-group v-model="form.deployType" @change="deployTypeChanged">
              <el-radio :label=1>soft</el-radio>
              <el-radio :label=2>system</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="软件/系统ID" prop="softSystemId">

            <el-select
              v-model.number="form.softSystemId"
              placeholder="请选择"
              style="width: 178px"
              @change="softSystemChanged"
            >
              <el-option
                v-for="item in softSystemsInfo"
                :key="item.id"
                :label="item.type+':'+item.name+':'+item.version"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="软件/系统名称" prop="softSystemName">
            <el-input v-model="form.softSystemName" readonly style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="部署方式" prop="deployWay">
            <el-radio-group
              v-model="form.deployWay"
              style="width: 178px">
              <el-radio :label=1>host</el-radio>
              <el-radio :label=2>docker</el-radio>
              <el-radio :label=3>k8s</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="服务器ID">
            <el-select
              v-model.number="form.serverId"
              placeholder="请选择"
              style="width: 178px"
              @change="serverChanged"
            >
              <el-option
                v-for="item in serversInfo"
                :key="item.id"
                :label="item.serverName+'('+item.serverNo+')'"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="服务器编号" prop="serverNo">
            <el-input v-model="form.serverNo" readonly style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="配置文件">
            <el-input v-model="form.configPath" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="数据目录">
            <el-input v-model="form.dataDir" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="日志目录">
            <el-input v-model="form.logDir" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="服务端口列表">
            <el-input v-model="form.endpoints" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="分配cpu">
            <el-input v-model="form.cpuAllocated" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="分配内存：单位G">
            <el-input v-model="form.memoryAllocated" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="分配磁盘：单位G">
            <el-input v-model="form.diskAllocated" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="访问url">
            <el-input v-model="form.accessUrl" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="机密信息">
            <el-input v-model="form.securityInfo" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="部署任务">
            <el-input v-model="form.taskDeploy" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备份任务">
            <el-input v-model="form.taskBackup" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="启动命令">
            <el-input v-model="form.cmdStart" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="停止命令">
            <el-input v-model="form.cmdStop" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="重启动命令">
            <el-input v-model="form.cmdRestart" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" rows="5" v-model="form.remark" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="负责用户" prop="ownerUserId">
            <el-select
              v-model.number="form.ownerUserId"
              placeholder="请选择"
              style="width: 178px"
            >
              <el-option
                v-for="item in users"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column fixed label="部署ID" prop="id"/>
        <el-table-column fixed label="软件/系统名称" prop="softSystemName"/>
        <el-table-column fixed label="服务器名称" prop="serverNo"/>
        <el-table-column label="部署类型" width="100px">
          <template slot-scope="scope">
            <span v-if="scope.row.deployType===1">soft</span>
            <span v-if="scope.row.deployType===2">system</span>
          </template>
        </el-table-column>
        <el-table-column label="部署方式" width="75px">
          <template slot-scope="scope">
            <span v-if="scope.row.deployWay===1">host</span>
            <span v-if="scope.row.deployWay===2">docker</span>
            <span v-if="scope.row.deployWay===3">k8s</span>
          </template>
        </el-table-column>
        <el-table-column label="配置文件" prop="configPath"/>
        <el-table-column label="数据目录" prop="dataDir"/>
        <el-table-column label="日志目录" prop="logDir"/>
        <el-table-column label="服务端口列表" prop="endpoints"/>
        <el-table-column label="机密信息" prop="securityInfo"/>
        <el-table-column label="负责用户" prop="ownerUser"/>
        <el-table-column label="创建用户" prop="createUser"/>
        <el-table-column label="创建时间" prop="createDate"/>
        <el-table-column label="更新时间" prop="updateDate"/>
        <el-table-column v-if="checkPer(['admin','server:edit','server:del'])" align="center" label="操作" width="150px">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudDeployment from '@/api/itrms/deployment'
import common from '@/api/itrms/common'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  softSystemId: null,
  softSystemName: null,
  serverId: null,
  serverNo: null,
  deployType: 1,
  deployWay: 1,
  configPath: null,
  dataDir: null,
  logDir: null,
  endpoints: null,
  cpuAllocated: null,
  memoryAllocated: null,
  diskAllocated: null,
  accessUrl: null,
  securityInfo: null,
  taskDeploy: null,
  taskBackup: null,
  cmdStart: null,
  cmdStop: null,
  cmdRestart: null,
  remark: null,
  ownerUserId: null,
  ownerUser: null
}
export default {
  name: 'Deployment',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '部署',
      url: 'api/itrms/deployment',
      idField: 'id',
      sort: 'id desc',
      crudMethod: {...crudDeployment}
    })
  },
  data() {
    return {
      serversInfo: [],
      softsInfo: [],
      systemsInfo: [],
      softSystemsInfo: [],
      users: [],
      permission: {
        add: ['admin', 'deployment:add'],
        edit: ['admin', 'deployment:edit'],
        del: ['admin', 'deployment:del']
      },
      rules: {
        softSystemId: [
          {required: true, message: '软件/系统ID不能为空', trigger: 'blur'}
        ],
        serverId: [
          {required: true, message: '服务器ID不能为空', trigger: 'blur'}
        ],
        ownerUserId: [
          {required: true, message: '负责用户不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    // 初始化获取列表数据
    this.initSelect();
  },
  methods: {
    // 新增编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      console.log("form", form)
      console.log("this.users", this.users)
    },
    // 提交前
    [CRUD.HOOK.beforeSubmit]() {
      // this.form.ownerUserId=null
      return true
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {

      return true
    },
    initSelect() {
      common.getUsers().then(res => {
        console.log('res', res)
        this.users = res.content
      })
      common.getServerInfos().then(res => {
        console.log('res', res)
        this.serversInfo = res.content
      })
      common.getSoftInfos().then(res => {
        console.log('res', res)
        let index
        for (index in res.content) {
          let softSystemInfo = {
            id: res.content[index].id,
            type: 'soft',
            name: res.content[index].softName,
            version: res.content[index].softVersion
          }
          this.softsInfo.push(softSystemInfo)
        }
      })
      common.getSystemInfos().then(res => {
        console.log('res', res)
        let index
        for (index in res.content) {
          let softSystemInfo = {
            id: res.content[index].id,
            type: 'system',
            name: res.content[index].systemName,
            version: res.content[index].systemVersion
          }
          this.systemsInfo.push(softSystemInfo)
        }
      })
      this.softSystemsInfo = this.softsInfo
    },
    deployTypeChanged(value) {
      console.log(value)
      if (value === 1) {
        this.softSystemsInfo = this.softsInfo
      } else {
        this.softSystemsInfo = this.systemsInfo
      }
    },
    softSystemChanged(value) {
      console.log(value)
      let index
      for (index in this.softSystemsInfo) {
        console.log("this.softSystemsInfo[" + index + "] = " + this.softSystemsInfo[index].id);
        if (this.softSystemsInfo[index].id === value) {
          this.crud.form.softSystemName = this.softSystemsInfo[index].name
        }
      }
    },
    serverChanged(value) {
      console.log(value)
      let index
      for (index in this.serversInfo) {
        console.log("this.serversInfo[" + index + "] = " + this.serversInfo[index].id);
        if (this.serversInfo[index].id === value) {
          this.crud.form.serverNo = this.serversInfo[index].serverNo
        }
      }
    }
  }
}
</script>


